package com.smcdd.cdb.dbo.cdb_dbo.network;

import android.content.Context;

import com.google.gson.GsonBuilder;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Completable;

/**
 * 作者：Administrator on 2017/6/8 11:02
 * 邮箱：zhanghuaiha@gmail.com
 */

public class RetrofitHelper {

    private Context mCntext;

    GsonConverterFactory factory = GsonConverterFactory.create(new GsonBuilder().create());
    private static RetrofitHelper instance = null;
    private Retrofit mRetrofit = null;
    public static RetrofitHelper getInstance(Context context){
        if (instance == null){
            instance = new RetrofitHelper(context);
        }
        return instance;
    }
    private RetrofitHelper(Context mContext){
        mCntext = mContext;
        init();
    }

    private void init() {
        provideHotApi();
    }

    private void resetApp() {
//        mRetrofit = new Retrofit.Builder()
//                .baseUrl("https://www.wacdd.com")
//                .client(OKHttpFactory.INSTANCE.getOkHttpClient())
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .build();

    }
    public RetrofitService getServer(){
        return mRetrofit.create(RetrofitService.class);
    }


    public OkHttpClient provideOKHttpClient() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(60000, TimeUnit.MILLISECONDS)
                .connectTimeout(60000, TimeUnit.MILLISECONDS)
                .addInterceptor(loggingInterceptor)
                .addInterceptor(new AuthInterceptor())
                .addInterceptor(new ResponseInterceptor())
                .build();


        return okHttpClient;
    }

    public void provideHotApi() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(HttpConstants.BaseURL_DEV)
                .client(provideOKHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }


}
